<template>
  <div class="login">
    <div class="left">
      <!-- 头部 -->
      <div class="login-title">
        <img class="t1" src="@/assets/img/login_logo.png" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>

      <!-- form -->
      <el-form :model="form" class="form" :rules="rules" ref="form">

        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>

        <el-form-item  prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input v-model="form.code" prefix-icon="el-icon-key"></el-input>
            </el-col>
            <el-col :span="8">
              <img :src="baseUrl + codeUrl" alt class="login_code" @click="reclick" />
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            我已同意
            <el-link type="primary" class="isbo">用户协议</el-link>和
            <el-link type="primary" class="isbo">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="btn" @click="tologin">登录</el-button>
          <br />
          <el-button type="primary" class="btn" @click="toregister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/login_img.png" alt />
    </div>

    <register ref="register"></register>
  </div>
</template>

<script>
import register from './register'
import {userLogin} from '@/api/login'
import {saveLocal,getlocal} from '@/utils/local'
export default {
  components:{
    register
  },
  data() {
    
    return {
      baseUrl:process.env.VUE_APP_URL,
      codeUrl:'/captcha?type=login',
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: [],
      },
      rules:{
        phone: [
          {required:true,message:'请输入手机号',trigger:'change'},
          {min:11,max:11,message:'请输入11位手机号',trigger:'change'},
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password:[
          {required:true,message:'请输入密码',trigger:'change'},
          {min:6,max:12,message:'请输入6到12位密码',trigger:'change'}
        ],
        code: [
          {required:true,message:'请输入验证码',trigger:'change'},
          {min:4,max:4,message:'请输入4位验证码',trigger:'change'}
        ],
        isPass: [
          {required:true,message:'请勾选',trigger:'change'},
          
        ],
      }
    }
    
  },
  methods:{
    tologin(){
      this.$refs.form.validate(valid=>{
        if(valid){

          

        userLogin(this.form).then(res=>{
            saveLocal('token',res.data.data.token)
            this.$router.push('/layout')
            this.$message({
            showClose: true,
            message: '恭喜你，这是一条成功消息',
            type: 'success' 
            });
        })
        }else{

          this.$message({
          showClose: true,
          message: '错了哦，这是一条错误消息',
          type: 'error'
        });
      
        }
      })
    },
    toregister(){
      this.$refs.register.isShow = true
    },
    reclick(){
      this.codeUrl = '/captcha?type=login' + Math.random()*999
    }
  },
  created() {
    if(getlocal('token')){
      this.$router.push('/layout')
    }
  },
};
</script>

<style lang='less'>
.login {
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 42px;
    .login-title {
      display: flex;
      align-items: center;
      .t1 {
        width: 25px;
        height: 18px;
      }
      .t2 {
        margin: 0 14px;
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .t3 {
        margin-left: 12px;
        width: 1px;
        height: 28px;
        background: #c7c7c7;
      }
      .t4 {
        margin-left: 12px;
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 30px;
      .login-code {
        width: 100%;
        height: 40px;
      }
      .btn {
        display: block;
        width: 100%;
      }
      .btn:nth-child(2) {
        margin-top: 25px;
      }
      .isbo {
        vertical-align: bottom;
      }
      .login_code {
        width: 100%;
        height: 100%;
      }
    }
  }
  .el-dialog__header{            
        padding: 0;
        background: linear-gradient(to right, #02c4fa, #1395fa);
        font-size: 16px;
        height: 53px;
        line-height: 53px;
        text-align: center;
        color: #fff;
    }

    .avatar-uploader .el-upload {
          border: 1px dashed #d9d9d9;
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
          border-color: #409EFF;
        }
        .avatar-uploader-icon {
          font-size: 28px;
          color: #8c939d;
          width: 178px;
          height: 178px;
          line-height: 178px;
          text-align: center;
        }
        .avatar {
          width: 178px;
          height: 178px;
          display: block;
        }
        .upload-img{
          margin-left: 150px;
        }
}
</style>